package com.yaodian100;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.FileUtils;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.core.template.service.impl.VelocityServiceImpl;

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Module Name          : .YiekeConvert
 Module Description   :

 Date Created      : 2011/12/22
 Original Author   : jeff.ma
 Team              : yaodian100
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MODIFICATION HISTORY
 ------------------------------------------------------------------------------
 Date Modified       Modified by       Comments
 ------------------------------------------------------------------------------
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/**
 * @author jeff.ma
 * 
 */
public class YiekeConvert {

	protected static Logger logger = LoggerFactory.getLogger(YiekeConvert.class);

	/** default constructor */
	public YiekeConvert() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Map<String, List> products = new HashMap();
		String f = FileUtils
				.readFileToString(new File("E:/Workspace/yaodian100-midend/src/test/java/agentProductList.txt"));
		int sucess = 0, fail = 0;
		Set supplier = new HashSet();

		for (String p : f.split("\\*_\\*")) {
			List<String> pArray = new ArrayList();
			pArray.addAll(Arrays.asList(p.split("\\|\\|")));
			if ((pArray != null) && (pArray.size() == 18)) {
				//
				pArray.set(13, StringUtils.replace(pArray.get(13), "$$$", "&#10;"));
				pArray.set(14, StringUtils.replace(pArray.get(14), "$$$", "&#10;"));
				HashMap<String, String> c = new HashMap();
				for (String a : pArray.get(16).split("\\$\\$")) {
					String[] b = a.split(":");
					if (b.length == 3) {
						supplier.add(b[0]);
						if ("金华本仓".equals(b[0]) && (Integer.parseInt(b[2]) > 0)) {
							// if (b[1].indexOf("/") != -1) {
							// String[] d = b[1].split("\\|");
							// for (String e : d[1].split("/")) {
							// c.add(d[0] + "|" + e);
							// }
							// } else {
							if (c.get(b[1]) == null) {
								c.put(b[1], b[2]);
							} else {
								c.put(b[1], "" + (Long.valueOf(c.get(b[1])) + Long.valueOf(b[2])));
							}
						}
					} else {
						logger.debug("F:{}/{}", new Object[] { pArray.get(1), pArray.get(16) });
					}
				}

				pArray.set(16, c.size() > 0 ? StringUtils.join(c.keySet(), "&#10;") : pArray.get(16));
				pArray.set(17, StringUtils.replace(pArray.get(17), "|", "&#10;"));
				if (c.size() > 0) {
					pArray.set(16, StringUtils.join(c.keySet(), "&#10;"));
					pArray.add(StringUtils.join(c.values().toArray(), "&#10;"));
					products.put(pArray.get(1), pArray);
					sucess++;
				} else {
					fail++;
				}
				// logger.debug("S{}:{}", sucess, pArray[1]);
			} else {
				fail++;
				// logger.debug("F{}:{}/{}", new Object[] { fail, pArray.length, StringUtils.toJSON(pArray) });
			}
			// if (sucess + fail > 500) {
			// break;
			// }
		}
		logger.debug("supplier:{}", supplier);
		logger.debug("result:{}/{}", sucess, fail);

		// File
		TemplateService templateService = new VelocityServiceImpl();
		Map objs = new HashMap();
		objs.put("products", products.values());
		objs.put("rowSize", products.size() + 1);
		String excel = templateService.format("yieke/productTxtToExcel", objs);
		File file = new File("YiekeProduct_" + DateUtil.convertDateToString("yyyyMMdd-HH", new Date()) + ".xls");
		FileUtils.writeStringToFile(file, excel);
	}
}
